package com.zh.work.controller.log;

import com.zh.common.aop.SpecialAop;
import com.zh.common.entity.AreaInterceptLog;
import com.zh.common.entity.IpInterceptLog;
import com.zh.common.entity.SystemLog;
import com.zh.common.entity.TransIntercept;
import com.zh.work.entity.JunyuLogEntity;
import com.zh.work.entity.channel.ChannelLogEntity;
import com.zh.work.entity.channel.TongLianLogEntity;
import com.zh.work.service.platform.channel.*;
import com.zh.work.util.plateform.DowLoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @version 1.0
 * @Description :  日志下载 controller
 * @Author : chenfei
 * @Create in : 2020/10/26 14:38
 */
@Api(value = "DownloadLogController", produces = "application/json;charset=UTF-8",tags = {"日志下载 controller"})
@RestController
@RequestMapping("/downloadLog")
public class DownloadLogController {

    @Autowired
    private RiskTransInterceptLogService riskTransInterceptLogService;

    @Autowired
    private JunyuLogService junyuLogService;

    @Autowired
    private ChannelLogService channelLogService;

    @Autowired
    private IpInterceptLogService ipInterceptLogService;

    @Autowired
    private AreaInterceptLogService areaInterceptLogService;

    @Autowired
    private TongLianLogService tongLianLogService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation(value = "风控日志下载" , tags = {"getRiskLogDownload"} , notes = "风控日志下载")
    @GetMapping("/getRiskLogDownload")
    public void getRiskLogDownload(
            @ApiParam(value = "开始日期", name = "addStartTime")  String addStartTime,
            @ApiParam(value = "结束日期", name ="addEndTime") String addEndTime,
            @ApiParam(value = "风控日志记录实体信息", name = "param") TransIntercept param, HttpServletResponse response) throws IOException {
            List<TransIntercept> data = riskTransInterceptLogService.selectRiskTransLogDownLoad(addStartTime,addEndTime,param);
            DowLoadUtil.dowLoadUtil("商户风控日志记录","商户风控日志记录",response,data,TransIntercept.class);
    }

    @ApiOperation(value = "骏聿日志下载" , tags = {"getJunYuLogDownload"} , notes = "骏聿日志下载")
    @GetMapping("/getJunYuLogDownload")
    public void getJunYuLogDownload(
            @ApiParam(value = "开始日期", name = "addStartTime")  String addStartTime,
            @ApiParam(value = "结束日期", name ="addEndTime") String addEndTime,
            @ApiParam(value = "骏聿日志实体信息", name = "JunyuLogEntity") JunyuLogEntity junyuLogEntity, HttpServletResponse response) throws IOException {
        List<JunyuLogEntity> data = junyuLogService.selectJunyuLogDownLoad(addStartTime,addEndTime,junyuLogEntity);
        DowLoadUtil.dowLoadUtil("骏聿日志记录","骏聿日志记录",response,data,JunyuLogEntity.class);
    }

    @ApiOperation(value = "ip黑名单与敏感地区检测日志下载" , tags = {"getIpInterceptLogDownload"} , notes = "ip黑名单与敏感地区检测日志下载")
    @GetMapping("/getIpInterceptLogDownload")
    public void getIpInterceptLogDownload(
            @ApiParam(value = "开始日期", name = "addStartTime") String addStartTime,
            @ApiParam(value = "结束日期", name ="addEndTime") String addEndTime,
            @ApiParam(value = "ip黑名单与敏感地区检测日志实体信息", name = "ipInterceptLog") IpInterceptLog ipInterceptLog,
            HttpServletResponse response) throws IOException {
        List<IpInterceptLog> data = ipInterceptLogService.getIpInterceptLogDown(addStartTime,addEndTime,ipInterceptLog);
        DowLoadUtil.dowLoadUtil("ip黑名单与敏感地区检测日志记录","ip黑名单与敏感地区检测日志记录",response,data,IpInterceptLog.class);
    }

    @ApiOperation(value = "限制地区检测日志下载" , tags = {"getAreaInterceptLogownload"} , notes = "限制地区检测日志下载")
    @GetMapping("/getAreaInterceptLogownload")
    public void getAreaInterceptLogownload(
            @ApiParam(value = "开始日期", name = "addStartTime") String addStartTime,
            @ApiParam(value = "结束日期", name ="addEndTime") String addEndTime,
            @ApiParam(value = "限制地区检测日志实体信息", name = "areaInterceptLog") AreaInterceptLog areaInterceptLog,
            HttpServletResponse response) throws IOException {
        List<AreaInterceptLog> data = areaInterceptLogService.getAreaInterceptLogDown(addStartTime,addEndTime,areaInterceptLog);
        DowLoadUtil.dowLoadUtil("限制地区检测日志记录","限制地区检测日志记录",response,data,AreaInterceptLog.class);
    }

    @ApiOperation(value = "二维码渠道日志下载" , tags = {"getChannelLogDownload"} , notes = "二维码渠道日志下载")
    @GetMapping("/getChannelLogDownload")
    public void getChannelLogDownload(
            @ApiParam(value = "渠道日志实体类", name = "channelLogEntity") ChannelLogEntity channelLogEntity,
            HttpServletResponse response) throws IOException {
        List<ChannelLogEntity> resultData = channelLogService.selectChannelLogDownLoad(channelLogEntity);
        String[] header  = new String[]{"商户号/订单号","渠道编号","渠道编码","所属交易 1-QRCode 2-快捷 3-网关 4-代付","请求状态 0-请求 1-银行返回","内容详情","请求地址","添加时间"};
        //创建excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("二维码渠道日志");
        //设置表格列宽度为20个字节
        sheet.setDefaultColumnWidth(20);
        HSSFRow headrow = sheet.createRow(0);
        for (int i = 0; i < header.length; i++) {
            HSSFCell cell = headrow.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(header[i]);
            cell.setCellValue(text);
        }
        for(int i=0;i<resultData.size();i++){
            if(Objects.equals(i,60000)){//限制导出数量不超6万条，超过65535条会报错
                break;
            }
            ChannelLogEntity channelLog = resultData.get(i);
            HSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(channelLog.getOrderno());
            row1.createCell(1).setCellValue(channelLog.getChannelId());
            row1.createCell(2).setCellValue(channelLog.getChannelCode());
            row1.createCell(3).setCellValue(channelLog.getTransType());
            row1.createCell(4).setCellValue(channelLog.getInput());
            row1.createCell(5).setCellValue(channelLog.getComDateStamp());
            row1.createCell(6).setCellValue(channelLog.getUrl());
            row1.createCell(7).setCellValue(channelLog.getAddDate());
        }

        String fileName = "历史记录";
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xls");
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    @ApiOperation(value = "通联进件日志下载" , tags = {"getTongLianLogDownload"} , notes = "通联进件日志下载")
    @GetMapping("/getTongLianLogDownload")
    public void getTongLianLogDownload(
            @ApiParam(value = "开始日期", name = "addStartTime") String addStartTime,
            @ApiParam(value = "结束日期", name ="addEndTime") String addEndTime,
            @ApiParam(value = "渠道日志实体类", name = "tongLianLogEntity") TongLianLogEntity tongLianLogEntity,
            HttpServletResponse response) throws IOException {
        List<TongLianLogEntity> resultData = tongLianLogService.getTongLianLogDownload( addStartTime,addEndTime  , tongLianLogEntity);
        String[] header  = new String[]{"下游提交原始参数","请求参数信息","返回参数信息","响应时间段","方法名","响应状态","创建时间","日志级别","备注","平台商户号","渠道商户号","客户商户号"};
        //创建excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("通联进件日志");
        //设置表格列宽度为20个字节
        sheet.setDefaultColumnWidth(20);
        HSSFRow headrow = sheet.createRow(0);
        for (int i = 0; i < header.length; i++) {
            HSSFCell cell = headrow.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(header[i]);
            cell.setCellValue(text);
        }
        for(int i=0;i<resultData.size();i++){
            if(Objects.equals(i,60000)){//限制导出数量不超6万条，超过65535条会报错
                break;
            }
            TongLianLogEntity tongLianLog = resultData.get(i);
            HSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(tongLianLog.getParams());
            row1.createCell(1).setCellValue(tongLianLog.getResult());
            row1.createCell(2).setCellValue(tongLianLog.getTimes());
            row1.createCell(3).setCellValue(tongLianLog.getMethod());
            row1.createCell(4).setCellValue(Objects.equals(tongLianLog.getSuccess(),1) ? "成功" : "失败");
            row1.createCell(5).setCellValue(tongLianLog.getCreateDate());
            row1.createCell(6).setCellValue(tongLianLog.getLevel());
            row1.createCell(7).setCellValue(tongLianLog.getReqParams());
            row1.createCell(8).setCellValue(tongLianLog.getRemark());
            row1.createCell(9).setCellValue(tongLianLog.getPlaMrcNo());
            row1.createCell(10).setCellValue(tongLianLog.getChannelMrcNo());
            row1.createCell(11).setCellValue(tongLianLog.getCustomerNo());
        }
        String fileName = "历史记录";
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xls");
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

}
